package com.example.liunanxin.lnx226ordering.model;

import com.example.liunanxin.lnx226ordering.beans.AllShopsBean;
import com.example.liunanxin.lnx226ordering.iface.ShopListListener;
import com.example.liunanxin.lnx226ordering.iface.Shopiface;
import com.example.liunanxin.lnx226ordering.retrofitservice.ShopService;
import com.example.liunanxin.lnx226ordering.url.Url;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ShopModel implements Shopiface {
    private Retrofit retrofit;

    public ShopModel(){
        retrofit=new Retrofit.Builder()
                .baseUrl(Url.USERURL)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }

    @Override
    public void getShopResult(final ShopListListener shopListListener) {
        ShopService shopService=retrofit.create(ShopService.class);
        Call<List<AllShopsBean>> call=shopService.getAllShops();
        call.enqueue(new Callback<List<AllShopsBean>>() {
            @Override
            public void onResponse(Call<List<AllShopsBean>> call, Response<List<AllShopsBean>> response) {
                if (response.isSuccessful()&&response!=null){
                    shopListListener.onResponse(response.body());
                }
                else shopListListener.onFail("onresponse fail");
            }

            @Override
            public void onFailure(Call<List<AllShopsBean>> call, Throwable t) {
                shopListListener.onFail(t.toString());

            }
        });
    }
}
